package com.ctshk.rpc.air.util;

import com.ctshk.rpc.air.thirdparty.nanhua.autotksoap.*;
import com.ctshk.rpc.air.thirdparty.nanhua.common.Authenticate;
import com.ctshk.rpc.air.thirdparty.nanhua.comsionsoap.CommissionService;
import com.ctshk.rpc.air.thirdparty.nanhua.comsionsoap.CommissionServiceImpl;
import com.ctshk.rpc.air.thirdparty.nanhua.comsionsoap.GetFareMarkupRQ;
import com.ctshk.rpc.air.thirdparty.nanhua.comsionsoap.GetFareMarkupRS;
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * 南华机票工具类
 * 2020-03-02
 */
@Slf4j
public class AirApiNanHwaUtil {

   private static String COMPANY_ID = "ACHI0023";
   private static String LOGIN_ID = "NHAPI";
   private static String PASSWORD ="chinatravel";
   private static String COMMONSION_WSDL ="http://ats.nanhwa.com:8089/CommissionAPI/ws/AirService?wsdl";
   private static String AUTOTICKET_WSDL ="http://ats.nanhwa.com:8082/AIR_WS_TKT/ws/AirService?wsdl";


   private static CommissionService commissionService;
   private static AutoTicketService autoTicketService;
   private static Authenticate authenticate;
   static {
       commissionService = buildCommissionService();
       autoTicketService = buildAutoTicketService();
       authenticate = buildAuth();
   }
    /**
     * 创建登录信息  暂时 commsions api 和 auto ticke api 是一样的 后面生产环境不一样时  需要分开初始化
     * @return
     */
   public static Authenticate buildAuth(){
       if(null==authenticate){
           authenticate = new Authenticate();
       }
       authenticate.setCompanyId(COMPANY_ID);
       authenticate.setLoginId(LOGIN_ID);
       authenticate.setPassword(PASSWORD);
       return  authenticate;
   }
    public static Authenticate buildAuth(String companyId,String loginId,String passWord){
        COMPANY_ID = companyId;
        LOGIN_ID = loginId;
        PASSWORD = passWord;
        return  buildAuth();
    }

    /**
     * 创建 commission Api Service
     * @return
     * @throws MalformedURLException
     */
    public static CommissionService buildCommissionService(){
       if(null==commissionService){
           try {
               URL url = new URL(COMMONSION_WSDL);
               CommissionServiceImpl commissionServiceImpl = new CommissionServiceImpl(url);
               commissionService = commissionServiceImpl.getAirServiceImplPort();
           }catch (MalformedURLException e){
                log.error("初始化南华机票--commission api 调用工具类 异常",e);
           }
       }
        return commissionService;
    }

    /**
     * 创建 AutoTicket Api Service
     * @return
     * @throws MalformedURLException
     */
    public static AutoTicketService buildAutoTicketService(){
        if(null==autoTicketService){
            try{
                URL url = new URL(AUTOTICKET_WSDL);
                AutoTicketServiceImpl autoTicketServiceImpl = new AutoTicketServiceImpl(url);
                autoTicketService = autoTicketServiceImpl.getAirServiceImplPort();
            }catch (MalformedURLException e){
                log.error("初始化南华机票--aotuticket api 调用工具类 异常",e);
            }
        }
        return autoTicketService;
    }

    /**
     *  南华机票 查询政策
     * @param flightOffersSearchs amadues差价返回结果 注释原因:测试时直接使用amadues的查价嫁接口进行请求  暂时注释 后面根据业务情况决定是否打开注释
     * @return
     */
//    public static GetFareMarkupRS getFareMarkupRS(FlightOfferSearch[] flightOffersSearchs){
//        GetFareMarkupRQ getFareMarkupRQ =assmbleMarkupListRq(flightOffersSearchs);
//        return  getFareMarkupRS(getFareMarkupRQ);
//    }


    /**
     *  南华机票 查询政策
     * @param getFareMarkupRQ
     * @return
     */
    public static GetFareMarkupRS getFareMarkupRS(GetFareMarkupRQ getFareMarkupRQ){
        GetFareMarkupRS getFareMarkupRS = commissionService.getFareMarkup(getFareMarkupRQ);
        return  getFareMarkupRS;
    }
    /**
     *  南华机票 自动出票
     * @param issueTicketRQ
     * @return
     */
    public static IssueTicketRS  issueTicket(IssueTicketRQ issueTicketRQ){
        IssueTicketRS issueTicketRS = autoTicketService.issueTicket(issueTicketRQ);
        return  issueTicketRS;
    }

    /**
     *  南华机票 废除机票
     * @param voidTicketRQ
     * @return
     */
    public static VoidTicketRS  voidTicket(VoidTicketRQ voidTicketRQ){
        VoidTicketRS voidTicketRS = autoTicketService.voidTicket(voidTicketRQ);
        return  voidTicketRS;
    }

    /**
     *  南华机票 检查机票状态
     * @param checkTicketingStatusRQ
     * @return
     */
    public static CheckTicketingStatusRS checkTicketingStatus(CheckTicketingStatusRQ checkTicketingStatusRQ){
        CheckTicketingStatusRS  checkTicketingStatusRS = autoTicketService.checkTicketingStatus(checkTicketingStatusRQ);
        return  checkTicketingStatusRS;
    }

    /**
     * 通过amadues的查询结果 组装 南华加幅接口参数 注释原因:暂时注释 后面根据业务情况决定是否打开注释
     * @return
     */
//    public static  GetFareMarkupRQ  assmbleMarkupListRq(FlightOfferSearch[] flightOffersSearchs){
//        GetFareMarkupRQ getFareMarkupRQ  = new GetFareMarkupRQ();
//        GetFareMarkupRQ.MarkupList markupList = new GetFareMarkupRQ.MarkupList();
//        for (int i = 0; i< flightOffersSearchs.length;i++){
//            FlightOfferSearch flightOfferSearch = flightOffersSearchs[i];
//            FareMarkupParameter fareMarkupParameter = new FareMarkupParameter();
//            fareMarkupParameter.setKey(flightOfferSearch.getId());
//            fareMarkupParameter.setMarketingAirline(flightOfferSearch.getValidatingAirlineCodes()[0]);
//            fareMarkupParameter.setFareType(flightOfferSearch.getPricingOptions().getFareType()[0]);
//            fareMarkupParameter.setFareAmount(flightOfferSearch.getPrice().getBase());
//            FareMarkupParameter.BookingClassList bookingClassList = new FareMarkupParameter.BookingClassList();
//            bookingClassList.getClassCode().add(flightOfferSearch.getTravelerPricings()[0].getFareDetailsBySegment()[0].getSegmentClass());
//            fareMarkupParameter.setBookingClassList(bookingClassList);
//            FareMarkupParameter.PaxTypeList paxTypeList = new FareMarkupParameter.PaxTypeList();
//            paxTypeList.getPaxType().add("1");
//            fareMarkupParameter.setPaxTypeList(paxTypeList);
//            fareMarkupParameter.setClassType(flightOfferSearch.getTravelerPricings()[0].getFareDetailsBySegment()[0].getCabin());
//            fareMarkupParameter.setDepartureCity(flightOfferSearch.getItineraries()[0].getSegments()[0].getDeparture().getIataCode());
//            fareMarkupParameter.setDestinationCity(flightOfferSearch.getItineraries()[0].getSegments()[0].getArrival().getIataCode());
//            DateTime dateTime  =new DateTime();
//            dateTime.setDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//            //dateTime.setTime("1400");
//            fareMarkupParameter.setDepartureDate(dateTime);
//            fareMarkupParameter.setTripType("OW");
//            markupList.getFareMarkupParameter().add(fareMarkupParameter);
//        }
//        getFareMarkupRQ.setMarkupList(markupList);
//        return  getFareMarkupRQ;
//    }





}


